package com.inifitness.tenant.core.security.service;

import com.inifitness.tenant.core.security.model.BizUserAcctDetails;
import com.inifitness.tenant.core.system.model.dto.UserAuthInfo;
import com.inifitness.tenant.core.system.service.BizUserAcctService;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 系统用户认证
 *
 * @author sunjinfeng
 * @since 2024/04/18 11:00:00
 */
@Service
@RequiredArgsConstructor
public class BizUserAcctDetailsService implements UserDetailsService {

  private final BizUserAcctService bizUserAcctService;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    UserAuthInfo userAuthInfo = bizUserAcctService.getUserAuthInfo(username);
    if (userAuthInfo == null) {
      throw new UsernameNotFoundException(username);
    }
    return new BizUserAcctDetails(userAuthInfo);
  }
}
